# Maintainer: Noel Kuntze <noel.kuntze@contauro.com>
pkgname=jitsi-meet
pkgver=2.0.8719
# the releases prefix the tag
_pkgver="jitsi-meet_${pkgver##*.}"
pkgrel=0
pkgdesc="WebRTC JavaScript video conferences"
url="https://github.com/jitsi/jitsi-meet"
arch="x86_64"
license="Apache-2.0"
depends="jicofo jitsi-videobridge nginx"
makedepends="npm"
subpackages="$pkgname-doc"
source="$pkgname-$pkgver.tar.gz::https://github.com/jitsi/jitsi-meet/archive/stable/$_pkgver.tar.gz
	openssl3.patch
	"
builddir="$srcdir/$pkgname-stable-$_pkgver"
options="!check" # No test suite

build() {
	npm ci
	make
	make source-package
}

package() {
	DESTDIR="$pkgdir"/usr/share/$pkgname
	CONFDIR="$pkgdir"/etc/jitsi/meet

	install -d "$DESTDIR"
	install -d "$CONFDIR"

	tar xjf "$pkgname.tar.bz2" -C "$DESTDIR" --strip 1
	install -Dm644 -t "$DESTDIR" manifest.json

	# Hack https://github.com/jitsi/jitsi-meet/pull/6925
	for l in $(node -p "Object.keys(require('./lang/languages.json')).join(' ')"); do
		c=${l:0:2}
		if [ -f "node_modules/i18n-iso-countries/langs/$c.json" ]; then
			install -m644 "node_modules/i18n-iso-countries/langs/$c.json" "$DESTDIR/lang/countries-$l.json"
		fi
	done

	# get rid of all local references
	find "$DESTDIR" -type f -name "*.js" -exec sed -i "s#$builddir##g" "{}" \+

	# Non-deterministic race in npm gives 777 permissions to random directories.
	# See https://github.com/npm/npm/issues/9359 for details.
	chmod -R 755 "$DESTDIR"

	# configuration files
	for i in interface_config.js config.js; do
		install -Dm644 "$DESTDIR/$i" "$CONFDIR/$i"
		ln -sf "/etc/jitsi/meet/$i" "$DESTDIR/$i"
	done

	# documentation
	install -Dm644 -t "$pkgdir/usr/share/doc/$pkgname" doc/debian/jitsi-meet/jitsi-meet.example doc/debian/jitsi-meet/jitsi-meet.example-apache config.js
	sed -i "s@/usr/share/jitsi-meet@/usr/share/webapps/$pkgname@" "$pkgdir/usr/share/doc/$pkgname/"*
	sed -i "s@/etc/jitsi/meet@/etc/webapps/$pkgname@" "$pkgdir/usr/share/doc/$pkgname/"*

	# npm gives ownership of ALL FILES to build user
	# https://bugs.archlinux.org/task/63396
	chown -R root:root "$pkgdir"
}

sha512sums="
401bd3ff108aa7838348d8015ced4835f4044f38d6b6c02e2aed4992689393f1f88b490393bff598ac4b80421a92b016b57fd7463e5cb81329c5896f9cb5e8bb  jitsi-meet-2.0.8719.tar.gz
f752d2eafd7f4e3a331c92b8ad25e6c150d608303fe74f32950a8deb55784853bd7225923d71ab55778b828955e150058e725d85995e4e6f776855015e4e16a9  openssl3.patch
"
